package com.biblioteca.model.tableModels;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.biblioteca.model.entities.Emprestimo;
import com.biblioteca.view.FramePrincipal;


@SuppressWarnings("serial")
public class EmprestimoTableModel extends AbstractTableModel {

	private Statement st ;
	private ResultSet rs ;
		
	private String nomes[] = {"ID Obra", "ID Cliente", "Data inicio", "Data para entrega"} ;
	public ArrayList<Emprestimo> emprestimos = new ArrayList<Emprestimo>() ;
	public ArrayList<Emprestimo> emprestimosAux = emprestimos;
	
	public EmprestimoTableModel (FramePrincipal fp) {

		try {
			st = fp.conn.createStatement() ;
			rs = st.executeQuery("SELECT * FROM Emprestimo;");	
			while (rs.next()) {
				emprestimos.add( new Emprestimo (rs.getInt(1), rs.getInt(2), rs.getDate(3), rs.getDate(4)) ) ;
			}
			st.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public int getColumnCount() {
		return 4;
	}

	@Override
	public int getRowCount() {
		return emprestimos.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		Emprestimo e = emprestimos.get(row) ;
		switch (column) {
		case 0:
			return e.getIdObra();
		case 1:
			return e.getIdCliente();
		case 2:
			return e.getInic();
		case 3:
			return e.getTerm();
		}
		return null;
	}

	@Override
	public String getColumnName(int column) {
		return nomes[column] ;
	}
}
